<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:bm="livcos.org/ns/example/bm"
  exclude-result-prefixes="bm">
	
	<xsl:param name="projRootId"/>
	
	<xsl:output method="xml" media-type="image/svg+xml"/>
    
  <xsl:template match="bm:*">
  </xsl:template>
  
  <xsl:template match="/">
    <xsl:apply-templates select="document(concat($projRootId,'/data/AppleCorp/BusinessPlan.view'))/*" />
  </xsl:template>
  
  <xsl:template match="/bm:business">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  
  <xsl:template match="bm:line">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  
  <xsl:template match="bm:model">
    <svg:svg width="780" height="140">
      <xsl:apply-templates select="bm:player"/>
    </svg:svg>
  </xsl:template>
  
  <xsl:template match="bm:player[1]">
    <xsl:variable name="x" select="260"/>
    <svg:rect x="{$x}" y="20" width="160" height="100" fill="lightblue" stroke="black" stroke-width="1"/>
    <svg:text x="{$x + 20}" y="60" fill="black" font-size="18">
      <xsl:value-of select="/*/bm:title"/>
    </svg:text>
  </xsl:template>
  
  <xsl:template match="bm:player[3]">
    <xsl:variable name="x" select="600"/>
    <xsl:variable name="w" select="160"/>
    <svg:rect x="{$x}" y="20" width="{$w}" height="100" fill="lightblue" stroke="black" stroke-width="1"/>
    <svg:text x="{$x + 20}" y="60" fill="black" font-size="18">
      <xsl:value-of select="bm:title"/>
    </svg:text>
    <xsl:apply-templates select="bm:relation">
      <xsl:with-param name="x1" select="420"/>
      <xsl:with-param name="x2" select="$x"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="bm:player[2]">
    <xsl:variable name="x" select="20"/>
    <xsl:variable name="w" select="120"/>
    <svg:rect x="{$x}" y="20" width="{$w}" height="100" fill="lightblue" stroke="black" stroke-width="1"/>
    <svg:text x="{$x + 20}" y="60" fill="black" font-size="18">
      <xsl:value-of select="bm:title"/>
    </svg:text>
    <xsl:apply-templates select="bm:relation">
      <xsl:with-param name="x1" select="260"/>
      <xsl:with-param name="x2" select="$x + $w"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="bm:relation">
    <xsl:param name="x1"/>
    <xsl:param name="x2"/>
    <xsl:call-template name="v-arrow">
      <xsl:with-param name="x1" select="$x1"/>
      <xsl:with-param name="x2" select="$x2"/>
      <xsl:with-param name="y" select="40"/>
      <xsl:with-param name="color" select="'green'"/>
      <xsl:with-param name="label">
        <xsl:apply-templates select="bm:input"/>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="v-arrow">
      <xsl:with-param name="x1" select="$x2"/>
      <xsl:with-param name="x2" select="$x1"/>
      <xsl:with-param name="y" select="70"/>
      <xsl:with-param name="color" select="'blue'"/>
      <xsl:with-param name="label">
        <xsl:apply-templates select="bm:payment"/>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="v-arrow">
      <xsl:with-param name="x1" select="$x1"/>
      <xsl:with-param name="x2" select="$x2"/>
      <xsl:with-param name="y" select="100"/>
      <xsl:with-param name="color" select="'red'"/>
      <xsl:with-param name="label">
        <xsl:apply-templates select="bm:need"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template name="v-arrow">
    <xsl:param name="x1"/>
    <xsl:param name="x2"/>
    <xsl:param name="y"/>
    <xsl:param name="color"/>
    <xsl:param name="label"/>
    <xsl:variable name="ax" select="if ($x1 > $x2) then $x2 + 14 else $x2 - 14"/>
    <xsl:variable name="tx" select="if ($x1 > $x2) then $x1 - 14 else $x1 + 14"/>
    <svg:line x1="{$x1}" y1="{$y}" x2="{$x2}" y2="{$y}" stroke="{$color}" stroke-width="1"/>
    <svg:polygon points="{$x2},{$y} {$ax},{$y - 5} {$ax},{$y + 5}" fill="{$color}"/>
    <svg:text x="{$tx}" y="{$y + 14}" fill="{$color}" font-size="14">
      <xsl:if test="$x1 > $x2">
        <xsl:attribute name="text-anchor">end</xsl:attribute>
      </xsl:if>
      <xsl:value-of select="$label"/>
    </svg:text>
  </xsl:template>
  
  <xsl:template match="bm:input | bm:need">
    <xsl:if test="position() > 1">, </xsl:if>
    <xsl:value-of select="." />
  </xsl:template>
  
  <xsl:template match="bm:payment">
    <xsl:if test="position() > 1">, </xsl:if>
    <xsl:text>$</xsl:text>
    <xsl:value-of select="@amount" />
    <xsl:text>/</xsl:text>
    <xsl:value-of select="if (normalize-space(@unit) != '') then @unit else 'unit'"/>
  </xsl:template>
  
</xsl:stylesheet>
